<template>
  <div>
    <van-picker title="标题"
      show-toolbar
      :columns="columns"
      :default-index="value"
      @confirm="onConfirm"
      @cancel="onCancel"
      @change="onChange" />
  </div>
</template>

<script>

import { updateUserProfiled } from '@/api/user.js'

export default {
  name: 'UpdataGender',
  props: {
    value: {
      type: Number,
      required: true
    }
  },
  data () {
    return {
      columns: ['男', '女'],
      localGender: this.value
    }
  },
  methods: {
    async onConfirm () {
      this.$toast.loading({
        message: '保存中...',
        forbidClick: true, // 禁止背景点击
        duration: 0 // 持续展示
      })

      try {
        const { data } = await updateUserProfiled({
          gender: this.localGender
        })
        console.log(data)

        // 更新视图
        this.$emit('input', this.localGender)
        console.log(11)
        // 关闭弹层
        this.$emit('close')

        // 提示成功
        this.$toast.success('更新成功')
      } catch (err) {
        this.$toast.fail('更新失败')
      }
    },
    // 选项改变时触发
    onChange (picker, value, index) {
      this.localGender = index
      console.log(index)
    },
    onCancel () {
      this.$emit('close')
    }
  }
}
</script>

<style>

</style>
